﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Jewelnoid
{
    class InputController
    {
        public KeyboardState keyboard;
        public KeyboardState oldKeyboard;
        public MouseState mouse;
        public MouseState oldMouse;

        public List<Keys> keysPressed;
        
        public InputController()
        {
            keysPressed = new List<Keys>();

            keyboard = Keyboard.GetState();
            mouse = Mouse.GetState();
        }

        public void getState()
        {
            oldKeyboard = keyboard;
            oldMouse = mouse;
            keyboard = Keyboard.GetState();
            mouse = Mouse.GetState();

            Keys[] keys = keyboard.GetPressedKeys();

            foreach (Keys keyPressed in keys)
            {
                if (!keysPressed.Contains(keyPressed))
                {
                    keysPressed.Add(keyPressed);
                }
            }
            
            foreach (Keys keyPressed in keysPressed)
            {
                if (keyboard.IsKeyUp(keyPressed))
                {
                    keysPressed.Remove(keyPressed);
                    break;
                }
            }
        }

        public bool isKeyJustPressed(Keys key)
        {
            if (keyboard.IsKeyDown(key) && oldKeyboard.IsKeyUp(key))
            {
                return true;
            }

            return false;
        }

        public bool isMouseOver(Text2D text)
        {
            if (mouse.X >= text.position.X && mouse.X <= text.position.X + text.width())
            {
                if (mouse.Y >= text.position.Y && mouse.Y <= text.position.Y + text.height())
                {
                    return true;
                }
            }

            return false;
        }

        public bool isMouseLeftButtonPressed()
        {
            return this.mouse.LeftButton.Equals(ButtonState.Pressed);
        }

        public bool isMouseLeftButtonJustPressed()
        {
            if (this.mouse.LeftButton.Equals(ButtonState.Pressed) && this.oldMouse.LeftButton.Equals(ButtonState.Released))
            {
                return true;
            }

            return false;
        }

        public bool isMouseLeftButtonPressed(Text2D text)
        {
            return isMouseLeftButtonPressed() && isMouseOver(text);
        }

        public bool isMouseLeftButtonJustPressed(Text2D text)
        {
            return isMouseLeftButtonJustPressed() && isMouseOver(text);
        }
    }
}
